<?php

session_start ();

if (! empty ( $_SESSION ['username'] )) {
	header ( "Location: index.php" );
	exit ();
}
$success = "";
if (! empty ( $_POST ['username'] ) && ! empty ( $_POST ['password'] ) && ! empty ( $_POST ['name'] ) && ! empty ( $_POST ['email'] )) {
	$user = addslashes ( strtolower ( $_POST ['username'] ) );
	$pass = md5 ( $_POST ['password'] );
	$name = addslashes ( strtolower ( $_POST ['name'] ) );
	$email = addslashes ( $_POST ['email'] );
	$id_number = addslashes ( $_POST ['id_number'] );
	$gender = addslashes ( $_POST ['gender'] );
	include 'config.php';
	$connection = mysql_connect ( $host, $username, $password );
	if (! $connection) {
		die ( array (
				'success' => false,
				'errors' => 'Can\'t connect to mysql' 
		) );
	}
	
	mysql_select_db ( "mydb", $connection ) or die ( json_encode ( array (
			'success' => false,
			'errors' => "could not connect to MySql" . mysql_error () 
	) ) );
	
	$query = "INSERT INTO User (username, name, email, password , id_number, gender, balance) VALUES ('$user' , '$name' , '$email' , '$pass' , '$id_number', '$gender' , 0) ";
	$result = mysql_query ( $query, $connection );
	
	if (! $result) {
		echo $query;
		die ( json_encode ( array (
				'success' => false,
				'errors' => 'Query errors' 
		) ) );
	}
	$success = "Register Successful";
	
	header ( "Location: index.php" );
	exit ();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Registration Form</title>
<link rel="STYLESHEET" type="text/css" href="../css/fg_membersite.css" />
<script type='text/javascript' src='../scripts/gen_validatorv31.js'></script>
<link rel="STYLESHEET" type="text/css" href="../css/pwdwidget.css" />
<script src="../scripts/pwdwidget.js" type="text/javascript"></script>
</head>
<body>

	<!-- Form Code Start -->
	<div id='fg_membersite'>
		<form id='register' action='<?php  echo $_SERVER['PHP_SELF'] ;?>'
			method='post' accept-charset='UTF-8'>
			<fieldset>
				<legend>Register</legend>

				<input type='hidden' name='submitted' id='submitted' value='1' />

				<div class='short_explanation'>* required fields</div>
				<input type='text' class='spmhidip' name='<?php ?>' />

				<div>
					<span class='error'><?php echo $success?></span>
				</div>
				<div class='container'>
					<label for='name'>Your Full Name*: </label><br /> <input
						type='text' name='name' id='name' maxlength="50" /><br /> <span
						id='register_name_errorloc' class='error'></span>
				</div>
				<div class='container'>
					<label for='id_number'>Your ID Number*: </label><br /> <input
						type='text' name='id_number' id='id_number' maxlength="50" /><br />
					<span id='register_name_errorloc' class='error'></span>
				</div>
				<div class='container'>
					<label for='gender'>Gender (M/FM)*:</label><br /> <input
						type='text' name='gender' id='gender' maxlength="50" /><br /> <span
						id='gender_errorloc' class='error'></span>
				</div>
				<div class='container'>
					<label for='email'>Email Address*:</label><br /> <input type='text'
						name='email' id='email' maxlength="50" /><br /> <span
						id='register_email_errorloc' class='error'></span>
				</div>
				<div class='container'>
					<label for='username'>UserName*:</label><br /> <input type='text'
						name='username' id='username' maxlength="50" /><br /> <span
						id='register_username_errorloc' class='error'></span>
				</div>
				<div class='container' style='height: 80px;'>
					<label for='password'>Password*:</label><br />
					<div class='pwdwidgetdiv' id='thepwddiv'></div>

					<input type='password' name='password' id='password' maxlength="50" />

					<div id='register_password_errorloc' class='error'
						style='clear: both'></div>
				</div>
				<div class='container'>
					<input type='submit' name='Submit' value='Submit' />
				</div>

			</fieldset>
		</form>
	</div>
</body>
</html>